package com.neusoft.ruangong.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.ruangong.domain.User;
import com.neusoft.ruangong.other.utils.R;
import com.neusoft.ruangong.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zhanglei
 * @create: 2021/06/29/11:05
 */
@Api(tags = {"用户模块"})
@Controller
@Slf4j
@CrossOrigin //解决跨域问题
public class IndexController {

    @Resource
    private UserService userService;


    //查询所有的用户
    @ApiOperation(value = "所有用户列表")
    @GetMapping("user/findAll")
    @ResponseBody
    public R findAllTeachers() {
        List<User> list = userService.list(null);
        return R.ok().data("items", list);
    }


    /**
     * 用户逻辑删除功能
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID逻辑删除用户")
    @DeleteMapping("user/{id}")
    @ResponseBody
    public R removeById(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @PathVariable String id) {
        boolean flag = userService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    @ApiOperation(value = "分页用户列表")
    @GetMapping("user/{page}/{limit}")
    @ResponseBody
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {

        Page<User> pageParam = new Page<>(page, limit);

        userService.page(pageParam, null);
        //每页的数据集合
        List<User> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "新增用户")
    @PostMapping("user/addTeacher")
    @ResponseBody
    public R save(
            @ApiParam(name = "teacher", value = "用户对象", required = true)
            @RequestBody User user) {
        User rUser = userService.queryUserByName(user.getName());
        if(rUser==null){
            return userService.save(user) ? R.ok().data("message","添加"+user.getName()+"用户成功") : R.error().data("message","添加失败，用户名重复！");
        }
        return R.error().data("message","添加失败，用户名重复！");
    }

    @ApiOperation(value = "根据ID查询用户")
    @GetMapping("user/getTeacher/{id}")
    @ResponseBody
    public R getById(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @PathVariable String id) {

        User user = userService.getById(id);
        return R.ok().data("item", user);
    }

    @ApiOperation(value = "修改用户")
    @PutMapping("user/updateTeacher")
    @ResponseBody
    public R updateById(
            @ApiParam(name = "teacher", value = "用户对象", required = true)
            @RequestBody User user) {

        boolean update = userService.updateById(user);
        if (update) {
            return R.ok();
        } else {
            return R.error();
        }

    }
    @ApiOperation(value = "根据name查询用户")
    @PutMapping("user/searchTeacher/{name}")
    @ResponseBody
    public R searchByName(
            @ApiParam(name = "name", value = "用户名", required = true)
            @PathVariable String name){
        User user = userService.queryUserByName(name);
        if(user.getId()!=null){
            return R.ok().data("user",user);
        }else {
            return R.ok().data("message","eror");
        }
    }

    @PostMapping("user/login")
    @ResponseBody
    public R main(User user, HttpSession session) {

        log.info("进入登录后台");
        log.info("传入参数为" + user.getName() + "   ==========" + user.getPassword());

        log.info("user:::=====" + user);


        User loginUser = userService.goLogin(user.getName(), user.getPassword(),user.getRole());
        System.out.println("loginUser = " + loginUser);
        //登录成功重定向到main.html
        if (loginUser != null) {
            //登录成功
            log.info("登录成功");
            session.setAttribute("loginUser", loginUser);
            return R.ok().data("result",true).data("msessage","登录成功");
        } else {
            //登录失败
            log.info("登录失败");
            return R.ok().data("result",false).data("msessage","登录失败账号或密码错误");
        }
    }
}
